package com.itcast.donkey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.adapters.ClickJourneyAdapter;
import com.itcast.donkey.modles.ClickJourneyModles;
import com.itcast.donkey.modles.UriModles;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/2.
 */
public class ClickJourneyActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout linScenicSpot;
    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private TextView tvTitleScenicSpot;
    private RecyclerView recyclerviewActivityClickJourney;

    private ClickJourneyModles modles;

    private ClickJourneyAdapter adapter;
    private int count;
    private String name;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_click_journey);
        initView();
        initData();
        initEvent();
    }

    private void initView() {

        linScenicSpot = (LinearLayout) findViewById(R.id.lin_scenic_spot);
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        tvTitleScenicSpot = (TextView) findViewById(R.id.tv_title_scenic_spot);
        recyclerviewActivityClickJourney = (RecyclerView) findViewById(R.id.recyclerview_activity_click_journey);
    }

    private void initData() {

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        count = intent.getIntExtra("count", 0);
        name=intent.getStringExtra("name");
        final String url = UriModles.ATTACK_CLICK_FRONT_DETAILED_TRAVEL_AREA_JOURNEY_URL + id + UriModles.ATTACK_CLICK_END_DETAILED_TRAVEL_AREA_JOURNEY_URL;

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String values = response.body().string();
                        Gson gson = new Gson();
                        modles = gson.fromJson(values, ClickJourneyModles.class);
                        if (modles.getAttraction_trips().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(ClickJourneyActivity.this, "获取网络失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    adapter = new ClickJourneyAdapter(ClickJourneyActivity.this, modles, count,name);
                    recyclerviewActivityClickJourney.setAdapter(adapter);

                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ClickJourneyActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerviewActivityClickJourney.setLayoutManager(linearLayoutManager);

                    break;
                case 2:
                    Toast.makeText(ClickJourneyActivity.this, "解析失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private void initEvent() {
        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                finish();
                break;
        }
    }
}
